<!--
 * @Author: swc shanwenchao12138@163.com
 * @Date: 2024-11-08 09:26:32
 * @LastEditors: zss
 * @LastEditTime: 2024-11-28 09:18:31
 * @FilePath: \vuexf\src\components\custom\homePage\homeCpt\homeProduct.vue
 * @Description: 
 * @Version: 2.0
 * @Autor: swc
-->
<template>
  <!-- :lg="12"
    :sm="24" -->
  <i-col
    :style="{
      flex: '0 0 calc(' + 25 * ffieldwidth + '%' + ' - ' + 10 + 'px)',
      height: (fheight || 300) + 'px'
    }"
  >
    <div class="home-page-box">
      <div class="home-page-header clear">
        <div class="home-page-header-left">
          <span>{{ $srv.getI18nByValue('产线展示信息') }}</span>
        </div>
      </div>

      <div class="home-page-body y-scroll">
        <Card style="width:100%" bordered>
          <template #title>
            <!-- <Icon type="ios-film-outline"></Icon> -->
            <!-- {{ $srv.getI18nBySrv('produc_line_detail_mgmt') }} -->
            {{ $srv.getI18nByValue('实时加工信息') }}
          </template>
          <template #extra>
            <div class="extra-filter">
              <span class="filter-title"
                >{{ $srv.getI18nByValue('产线编号') }}：</span
              >
              <Select
                v-if="producLineNumberList.length > 0"
                v-model="producLineNumber"
                @on-change="changeProducLineNumber"
                clearable
                style="width:200px"
              >
                <Option
                  v-for="item in producLineNumberList"
                  :value="item.value"
                  :key="item.value"
                  >{{ item.label }}</Option
                >
              </Select>
              <span class="right-title">{{
                $srv.getI18nBySrv('intel_sche_details')
              }}</span>
            </div>
          </template>
          <!-- <template #title> -->
          <!-- </template> -->
          <component
            :is="productLineMgmt.compName"
            ref="productLineMgmt"
            v-bind="{
              config: productLineMgmt.config
            }"
          ></component>
        </Card>
      </div>
    </div>
  </i-col>
</template>

<script lang="ts">
import { Tsrv, T_srv, ifs, ts, vue } from '@/services';
import { N_oBaseGrid } from '../config';

@T_srv.comp
export default class homeProduct extends vue {
  @T_srv.prop({})
  ffieldwidth: number;

  @T_srv.prop({})
  fheight: number;

  @T_srv.ref('productLineMgmt') refProductLineMgmt: N_oBaseGrid.oBaseGrid;

  /* 组件名 */
  private name: string = 'homeProduct';

  private G_compName: string = this.name;

  private productLineMgmt: any = {
    tableName: 'produc_line_detail_mgmt',
    compName: null,
    config: null
  };

  private producLineNumber: string = '-1';

  private producLineNumberList = [];

  created() {
    this.init();
  }

  mounted() {
    this.$bus.$on('UPD_PL_DETAILS', () => {
      this.refProductLineMgmt.GT_editor.loadTableData();
    });
  }

  private init() {
    this.getTablesConf(); // 获取表配置
  }

  private changeProducLineNumber() {
    const filter = new ts.T_filter();
    if (Tsrv.noValue(this.producLineNumber)) {
      this.refProductLineMgmt.GT_editor.clearFilter();
    } else {
      filter.filter = {
        master_bill_id: {
          $in: [this.producLineNumber]
        }
      };
      this.refProductLineMgmt.GT_editor.addInitFilter(filter);
    }
    this.refProductLineMgmt.GT_editor.loadTableData();
  }

  // // 获取产线编号
  private getProducLineNumberOptions() {
    if (!Tsrv.noValue(this.productLineMgmt?.config?.tableColumns)) {
      const masterField = this.productLineMgmt.config.tableColumns.find(
        e => e.field === 'master_bill_id'
      );
      if (!Tsrv.noValue(masterField?.many2one)) {
        const tableName = masterField.many2one.toTable;
        const agreedFields = [masterField.many2one.toField, 'id'];
        const filter = {};
        const funName = masterField.many2one.toTable;
        Tsrv.getDataWithOption(
          this.$i18n.locale || 'en_US',
          Tsrv.lexicon.globalVar.dbType.MYSQL,
          tableName,
          agreedFields,
          filter,
          funName
        ).then(([ok, res, _, err]) => {
          if (ok) {
            if (res.data.total > 0) {
              this.producLineNumberList = res.data.rows.map(e => {
                return {
                  label: e[masterField.many2one.toField],
                  value: e.id
                };
              });
            }
          } else {
            this.$msg(err);
          }
        });
      }
    }
  }

  // 取消增删改权限
  private removeToolbarButton(config: any) {
    if (!Tsrv.noValue(config?.toolbar)) {
      config.toolbar.insert = false;
      config.toolbar.save = false;
      config.toolbar.edit = false;
      config.toolbar.delete = false;
      if (!Tsrv.noValue(config?.toolbar?.addon)) {
        config?.toolbar?.addon.forEach(el => {
          el.initNotshow = true;
        });
      }
    }
  }

  // 更改字段配置
  private changeFieldConfig() {
    if (!Tsrv.noValue(this.productLineMgmt?.config?.tableColumns)) {
      const rows = this.productLineMgmt.config.tableColumns;
      rows.forEach(el => {
        if (['expect_end_time', 'expect_start_time'].includes(el.field)) {
          el.isShow = true;
        }
      });
    }
  }

  private getTablesConf() {
    this.$store
      .dispatch('getLayoutConfigAsync', {
        funid: null,
        field: 'produc_line_detail_mgmt',
        type: 'component'
      })
      .then(res => {
        if (res?.current?.config?.name === this.productLineMgmt.tableName) {
          this.productLineMgmt.config = Tsrv.utils.clone(
            res.current.config,
            true
          );
          this.removeToolbarButton(this.productLineMgmt.config);
          // 更改字段配置
          this.changeFieldConfig();
          this.productLineMgmt.compName = `o${res.current.name}`;
        }
        this.getProducLineNumberOptions(); // 获取产线编号
      })
      .catch(e => {
        console.error('请求数据出错', e);
      });
  }

  destroyed() {
    /* 注册组件销毁 */
  }
}
</script>
<style lang="scss" scoped>
::v-deep .ivu-card {
  width: 100%;
  height: 100%;
  border: 1px solid var(--multiform-toolbar-bg-color);
  border-bottom: none;
  .ivu-card-head {
    height: 36px;
    padding: 0 15px;
    line-height: 36px;
    font-weight: 700;
  }
  .ivu-card-body {
    padding: 0;
    height: calc(100% - 36px);
    .wy-grid {
      // border-bottom: none;
      border: none;
    }
  }
  .ivu-card-extra {
    top: 2px;
    .right-title {
      font-weight: 700;
      margin-left: 20px;
    }
    // .extra-filter {
    //   .filter-title {
    //     margin-right: 10px;
    //   }
    // }
  }
}
</style>
